import models from '../../../models'

export default defineEventHandler(async (event: any) => {
  try {
    const query = getQuery(event)
    const where: { level?: number; parentCode?: string } = {}
    
    if (query.level) {
      where.level = parseInt(query.level as string)
    }
    
    if (query.parentCode) {
      where.parentCode = query.parentCode as string
    }
    
    const regions = await models.Region.findAll({
      where,
      order: [['code', 'ASC']]
    })
    
    return { regions }
  } catch (error) {
    throw createError({
      statusCode: 500,
      message: '获取地区列表失败'
    })
  }
}) 